package com.shuchang.yygh.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuchang.yygh.common.result.Result;
import com.shuchang.yygh.common.utils.MD5;
import com.shuchang.yygh.model.hosp.HospitalSet;
import com.shuchang.yygh.model.service.HospitalSetService;
import com.shuchang.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin//跨域请求访问设置
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    //http://localhost:8201/admin/hosp/hospitalSet/findAll
    // 1、查询所有医院设置表信息
    @GetMapping("findAll")
    @ApiOperation("获取医院设置信息")
    public Result findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    // 2、逻辑删除医院设置
    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除医院设置")
    public Result removeHospitalSetById(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 3、条件查询带分页
    @PostMapping("findPageHospitalSet/{current}/{limit}")
    @ApiOperation("条件查询带分页")
    public Result findPageHospitalSet(@PathVariable Long current,
                                      @PathVariable Long limit,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        // 创建page对象，传递当前页，每页记录数
        Page page = new Page(current, limit);

        //构建条件
        QueryWrapper wrapper = new QueryWrapper<HospitalSet>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hoscode);
        }
        wrapper.orderByDesc("create_time");
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        return Result.ok(pageHospitalSet);
    }

    // 4、添加医院设置
    @PostMapping("saveHospitalSet")
    @ApiOperation("添加医院设置")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // status 设置状态 1：使用；0：不能使用
        hospitalSet.setStatus(1);
        Random random = new Random();
        String signKey = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(signKey);
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 5、根据id获取医院设置
    @GetMapping("getHospitalSet/{id}")
    @ApiOperation("根据id获取医院设置")
    public Result getHospSet(@PathVariable Long id) {
//        try {
//            //模拟异常
//            int i = 1 / 0;
//        } catch (Exception e) {
//            throw new YyghException("失败", 201);
//        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return Result.ok(hospitalSet);
    }

    // 6、修改医院设置
    @PostMapping("updateHospitalSet")
    @ApiOperation("修改医院设置")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean updateById = hospitalSetService.updateById(hospitalSet);
        if (updateById) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 7、批量删除医院设置
    @DeleteMapping("batchRemoveHospitalSet")
    @ApiOperation(value = "批量删除医院设置")
    public Result batchRemoveHospitalSet(@RequestBody List<String> list) {
        boolean removeByIds = hospitalSetService.removeByIds(list);
        if (removeByIds) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 8、医院设置锁定和解锁
    @PutMapping("lock/{id}/{status}")
    @ApiOperation("医院设置锁定和解锁")
    public Result lockAndUnlock(@ApiParam(name = "id", value = "医院设置ID", required = true)
                                @PathVariable("id") Long id,
                                @ApiParam(name = "status", value = "锁定状态（0：锁定 1：解锁）", required = true)
                                @PathVariable("status") Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (status == 0 || status == 1) {
            hospitalSet.setStatus(status);
            boolean updateById = hospitalSetService.updateById(hospitalSet);
            return Result.ok();
        } else {
            hospitalSet.setStatus(0);
            return Result.fail("状态非法，锁定状态（0：锁定 1：解锁）");
        }
    }

    // 9、发送签名秘钥
    @PutMapping("sendSingKey/{id}")
    @ApiOperation("发送签名秘钥")
    public Result sendSingKey(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();

        // TODO: 2021/11/26 发送短信
        return Result.ok();
    }

}
